﻿#include "QCefSetting.h"

//////////////////////////////////////////////////////////////////////////
//
QCefSetting* QCefSetting::ms_instance = nullptr;


//////////////////////////////////////////////////////////////////////////
//
QCefSetting::QCefSetting()
{
    QCEF_DEBUG();

    init();

    //保存第一个实例
    if (ms_instance == nullptr)
    {
        ms_instance = this;
    }
}

QCefSetting::~QCefSetting()
{
    QCEF_DEBUG();

    //
    if (ms_instance == this)
    {
        ms_instance = nullptr;
    }
}

int QCefSetting::init()
{
    QCEF_DEBUG();

    m_cefSettings.no_sandbox = true;

#ifdef QT_CEF_OSR // is QT_CEF_OSR
    m_cefSettings.windowless_rendering_enabled = true;
#else           //not QT_CEF_OSR
    m_cefSettings.windowless_rendering_enabled = false;
#endif          //end QT_CEF_OSR

#if (defined(DEBUG) || defined(_DEBUG) || defined(Q_DEBUG))
    m_cefSettings.log_severity = LOGSEVERITY_DISABLE;   //LOGSEVERITY_DEBUG;
#else
    m_cefSettings.log_severity = LOGSEVERITY_DISABLE;
#endif

#ifdef QT_CEF_MULTI_THREAD_MSG_LOOP
    m_cefSettings.multi_threaded_message_loop = true;
    m_cefSettings.external_message_pump = false;
#else
    m_cefSettings.multi_threaded_message_loop = false;
    m_cefSettings.external_message_pump = true;
#endif

    CefString(&m_cefSettings.resources_dir_path) = CefString();
    CefString(&m_cefSettings.locales_dir_path) = CefString();

    CefString(&m_cefSettings.locale) = "zh-CN";
    CefString(&m_cefSettings.accept_language_list) = "zh-CN";
    CefString(&m_cefSettings.user_agent) = "Mozilla/5.0 AppleWebKit/537.36 Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.5060.134";

    m_cefSettings.persist_session_cookies = true;
    m_cefSettings.persist_user_preferences = true;

    m_cefSettings.background_color = 0;

    return 0;
}


//////////////////////////////////////////////////////////////////////////
//
void QCefSetting::setNoSandbox(bool disable)
{
    m_cefSettings.no_sandbox = (disable ? 1 : 0);
}

bool QCefSetting::getNoSandbox()
{
    return (m_cefSettings.no_sandbox > 0 ? true : false);
}

void QCefSetting::setSubprocessPath(const QString & strFilePath)
{
    if (strFilePath.length() > 0 && QFile::exists(strFilePath))
    {
        CefString(&m_cefSettings.browser_subprocess_path).FromString(strFilePath.toStdString());
    }
}

QString QCefSetting::getSubprocessPath()
{
    return QString::fromStdString(CefString(&m_cefSettings.browser_subprocess_path).ToString());
}

void QCefSetting::setMultiThreadMsgLoop(bool bMultiThread)
{
    m_cefSettings.multi_threaded_message_loop = (bMultiThread ? 1 : 0);
}

bool QCefSetting::getMultiThreadMsgLoop()
{
    return (m_cefSettings.multi_threaded_message_loop > 0 ? true : false);
}

void QCefSetting::setExternalMsgPump(bool bExtPump)
{
    m_cefSettings.external_message_pump = (bExtPump ? 1 : 0);
}

bool QCefSetting::getExternalMsgPump()
{
    return (m_cefSettings.external_message_pump > 0 ? true : false);
}

void QCefSetting::setOsr(bool bOsr)
{
    m_cefSettings.windowless_rendering_enabled = (bOsr ? 1 : 0);
}

bool QCefSetting::getOsr()
{
    return (m_cefSettings.windowless_rendering_enabled > 0 ? true : false);
}

void QCefSetting::setCachePath(const QString & strPath)
{
    CefString(&m_cefSettings.cache_path).FromString(strPath.toStdString());
}

QString QCefSetting::getCachePath()
{
    return QString::fromStdString(CefString(&m_cefSettings.cache_path).ToString());
}

void QCefSetting::setRootCachePath(const QString & strPath)
{
    CefString(&m_cefSettings.root_cache_path).FromString(strPath.toStdString());
}

QString QCefSetting::getRooCachePath()
{
    return QString::fromStdString(CefString(&m_cefSettings.root_cache_path).ToString());
}

void QCefSetting::setUserDataPath(const QString & strPath)
{
    CefString(&m_cefSettings.user_data_path).FromString(strPath.toStdString());
}

QString QCefSetting::getUserDataPath()
{
    return QString::fromStdString(CefString(&m_cefSettings.user_data_path).ToString());
}

void QCefSetting::setPersistSessionCookies(bool bPersist)
{
    m_cefSettings.persist_session_cookies = (bPersist ? 1 : 0);
}

bool QCefSetting::getPersistSessionCookies()
{
    return (m_cefSettings.persist_session_cookies > 0 ? true : false);
}

void QCefSetting::setPersistUserPreferences(bool bPersist)
{
    m_cefSettings.persist_user_preferences = (bPersist ? 1 : 0);
}

bool QCefSetting::getPersistUserPreferences()
{
    return (m_cefSettings.persist_user_preferences > 0 ? true : false);
}

void QCefSetting::setUserAgent(const QString & strAgent)
{
    CefString(&m_cefSettings.user_agent).FromString(strAgent.toStdString());
}

QString QCefSetting::getUserAgent()
{
    return QString::fromStdString(CefString(&m_cefSettings.user_agent).ToString());
}

void QCefSetting::setUserAgentProduct(const QString & strAgent)
{
    CefString(&m_cefSettings.user_agent_product).FromString(strAgent.toStdString());
}

QString QCefSetting::getUserAgentProduct()
{
    return QString::fromStdString(CefString(&m_cefSettings.user_agent_product).ToString());
}

void QCefSetting::setLocale(const QString & strLocale)
{
    CefString(&m_cefSettings.locale).FromString(strLocale.toStdString());
}

QString QCefSetting::getLocale()
{
    return QString::fromStdString(CefString(&m_cefSettings.locale).ToString());
}

void QCefSetting::setLogFile(const QString & strLogFile)
{
    CefString(&m_cefSettings.log_file).FromString(strLogFile.toStdString());
}

QString QCefSetting::getLogFile()
{
    return QString::fromStdString(CefString(&m_cefSettings.log_file).ToString());
}

void QCefSetting::setLogSeverity(cef_log_severity_t iLogSeverity)
{
    m_cefSettings.log_severity = iLogSeverity;
}

cef_log_severity_t QCefSetting::getLogSeverity()
{
    return (m_cefSettings.log_severity);
}

void QCefSetting::setResourcesPath(const QString & strPath)
{
    CefString(&m_cefSettings.resources_dir_path).FromString(strPath.toStdString());
}

QString QCefSetting::getResourcesPath()
{
    return QString::fromStdString(CefString(&m_cefSettings.resources_dir_path).ToString());
}

void QCefSetting::setLocalesPath(const QString & strPath)
{
    CefString(&m_cefSettings.locales_dir_path).FromString(strPath.toStdString());
}

QString QCefSetting::getLocalesPath()
{
    return QString::fromStdString(CefString(&m_cefSettings.locales_dir_path).ToString());
}

void QCefSetting::setAcceptLanguage(const QString & strLanguage)
{
    CefString(&m_cefSettings.accept_language_list).FromString(strLanguage.toStdString());
}

QString QCefSetting::getAcceptLanguage()
{
    return QString::fromStdString(CefString(&m_cefSettings.accept_language_list).ToString());
}
